<?php
/*
DKret2 helper functions
*/

global $wp_version;
if ( version_compare($wp_version, '2.6', '<') && !function_exists('jt_gallery_shortcode')) {
	# shortcodes
	add_filter('get_the_excerpt', 'strip_shortcodes', 0);
	add_filter('get_the_excerpt', 'restore_shortcodes', 20);
		
	function strip_shortcodes($in)
	{
		remove_filter('the_content', 'do_shortcode', 11);
		return $in;
	} # strip_shortcodes()

		
	function restore_shortcodes($in)
	{
		add_filter('the_content', 'do_shortcode', 11);
		return $in;
	} # restore_shortcodes()
}
	
function exist_widget_column ($column) {
  $widgetcolumns = wp_get_sidebars_widgets();
	switch($column) {
		case 'all' :
		  foreach ($widgetcolumns as $sidebar)
				if (!empty($sidebar)) return true;
    	break;
		case 'bottombars' :
  		if (($widgetcolumns['sidebar-5']) || ($widgetcolumns['sidebar-6']) || ($widgetcolumns['sidebar-7'])) return true;
			break;
		case 'Primary Sidebar' :
			if ($widgetcolumns['sidebar-1']) return true;
    	break;
		case 'Sidebar Column1' :
			if ($widgetcolumns['sidebar-2']) return true;
    	break;
		case 'Sidebar Column2' :
			if ($widgetcolumns['sidebar-3']) return true;
    	break;
		case 'Secondary Sidebar' :
			if ($widgetcolumns['sidebar-4']) return true;
    	break;
		case 'Bottombar Column1' :
			if ($widgetcolumns['sidebar-5']) return true;
    	break;
		case 'Bottombar Column2' :
			if ($widgetcolumns['sidebar-6']) return true;
    	break;
		case 'Bottombar Column3' :
			if ($widgetcolumns['sidebar-7']) return true;
    	break;
  }
	return false;
}

function dkret_globalnav() {
	$options=get_option('dkret3');
	$dkret_is_front = get_option('page_on_front');
	$show_on_front = get_option('show_on_front');

	$menu = "\n\t\t<div id=\"menu\">\n\t\t\t<ul>\n";

	/* call out the blog page as a seperate li, so we can apply the class manually */
	if ($show_on_front == 'posts') {
		if (is_home() && !is_page($dkret_is_front)) {
			$menu .= "\t\t\t\t<li class=\"current_page_item page_item\">";
		} else {
			$menu .= "\t\t\t\t<li class=\"page_item\">";
		}
		$menu .= "<a href=\"". get_option('home') .'">'.__('Start','dkret').'</a></li>';
	}
	$menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages('title_li=&sort_column=menu_order&depth='. $options['navigationlevel'] .'&echo=0') );
	if ( ($options['gallery_cat_id'] != '-1') && (($options['gallerylink']) == '1') ) {
		$menu .= '<li class="page_item"><a href="'. get_category_link($options['gallery_cat_id']) .'">'.__('Gallery','dkret') .'</a></li>';
	}
	if ((function_exists('library_url')) && ($options['nowreading'] == 1))  {
		$menu .= '<li class="page_item"><a href="'. library_url(false) .'">'.__('My Books','dkret') .'</a></li>';
	}
	$menu .= "\n\t\t\t</ul>\n\t\t</div><!-- #menu -->\n";
	echo apply_filters( 'dkret_menu', $menu ); // Filter to override default globalnav
}

function dkret_version_check() {
	// check for a new version
	$options = get_option('dkret3');
	$check_intervall = $options['next_update'];
			
	if ( ($check_intervall < time() ) or (empty($check_intervall)) ) {
		if (class_exists(snoopy)) {
			$client = new Snoopy();
			$client->_fp_timeout = 10;
			if (@$client->fetch(DKRETUPDATE) === false) {
				return false;
			}
			
		   	$remote = $client->results;
		   	
			$server_version = unserialize($remote);
			if (is_array($server_version)) {
				if ( version_compare($server_version[1], DKRETVERSION, '>') )
				 	return true;
			} 
			// come back in 24 hours :-)
			$options['next_update'] = time() + 86400;
			update_option('dkret3', $options );
			return false;
		}				
	}
}

function findrandompicture() {
/* 
Original Plugin Name: K2-Picture upload
Original Plugin URI: http://ramlev.dk/wp-plugins/
Description: Change header picture.
Author: Hasse Ramlev Hansen <hasse@ramlev.dk>
Author URI: http://ramlev.dk
Version: 1.1.1
*/

	$picture_dir = @ dir(ABSPATH . '/wp-content/themes/' . get_template() . '/images/headers');
	if ($picture_dir)
	{
		while(($file = $picture_dir->read()) !== false)
		{
			if (!preg_match('|^\.+$|', $file) && !preg_match('|^CVS$|', $file) && preg_match('/\.(gif|jpg|png)$/i',$file))
			{
				$picture_files[] = $file;
			}
		}
	}
  $size = count($picture_files);
  return ($picture_files[rand(0, $size-1)]);
}

function dkret_inline_css() {
	$options = get_option('dkret3');
	if (DKRETSCHEME != 'standard')
		echo "\n\t<link rel=\"stylesheet\" type=\"text/css\" title=\"scheme\" media=\"screen\" href=\"". DKRETSCHEME .'" />';

	$picture = $options['headerimage'];
	if ($options['random_header'] == "1") {
		$picture = findrandompicture();
	}
	/* 
	check if picture is not the standard one and picture is not empty (otherwise right-click show graphic would open the header
	directory)
	*/
	if (($picture != "standard") && ($picture != "")) {
		echo "\n\t<style type=\"text/css\">#header { background: url('". get_bloginfo('template_url') ."/images/headers/$picture') repeat-x; }</style>";
	}
	if ($options['textalign'] == "1") {
		echo "\n\t<style type=\"text/css\">#content { text-align: justify; }</style>";
	}
	if ($options['imageborders'] == "1") {
		echo "\n\t<style type=\"text/css\">.hentry img { border: 1px solid #666; padding: 3px; }</style>";
	}
}

function dkret_update_dashboard() {
  if (!dkret_version_check())
    return;

	if ( current_user_can('manage_options') )
		printf(__('<div class="plugin-update">A new version of dkret3 is available! <a href="%1$s">Please update now.</a></div>', 'dkret'), THEMEURI);
	else
		printf(__('<div class="plugin-update">A new version of dkret3 is available! Please notify the site administrator.</div>', 'dkret'));
}

// Generates semantic classes for BODY element
function sandbox_body_class( $print = true ) {
       global $wp_query, $current_user;

       // It's surely a WordPress blog, right?
       $c = array('wordpress');

       // Applies the time- and date-based classes (below) to BODY element
       sandbox_date_classes( time(), $c );

       // Generic semantic classes for what type of content is displayed
       is_front_page()  ? $c[] = 'home'       : null; // New 'front' class for WP 2.5
       is_home()        ? $c[] = 'blog'       : null; // Class for the posts, if set
       is_archive()     ? $c[] = 'archive'    : null;
       is_date()        ? $c[] = 'date'       : null;
       is_search()      ? $c[] = 'search'     : null;
       is_paged()       ? $c[] = 'paged'      : null;
       is_attachment()  ? $c[] = 'attachment' : null;
       is_404()         ? $c[] = 'four04'     : null; // CSS does not allow a digit as first character

       // Special classes for BODY element when a single post
       if ( is_single() ) {
               $postID = $wp_query->post->ID;
               the_post();

               // Adds 'single' class and class with the post ID
               $c[] = 'single postid-' . $postID;

               // Adds classes for the month, day, and hour when the post was published
               if ( isset( $wp_query->post->post_date ) )
                       sandbox_date_classes( mysql2date( 'U', $wp_query->post->post_date ), $c, 's-' );

               // Adds category classes for each category on single posts
               if ( $cats = get_the_category() )
                       foreach ( $cats as $cat )
                               $c[] = 's-category-' . $cat->slug;

               // Adds tag classes for each tags on single posts
               if ( $tags = get_the_tags() )
                       foreach ( $tags as $tag )
                               $c[] = 's-tag-' . $tag->slug;

               // Adds MIME-specific classes for attachments
               if ( is_attachment() ) {
                       $mime_type = get_post_mime_type();
                       $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
                               $c[] = 'attachmentid-' . $postID . ' attachment-' . str_replace( $mime_prefix, "", "$mime_type" );
               }

               // Adds author class for the post author
               $c[] = 's-author-' . sanitize_title_with_dashes(strtolower(get_the_author_login()));
               rewind_posts();
       }

       // Author name classes for BODY on author archives
       elseif ( is_author() ) {
               $author = $wp_query->get_queried_object();
               $c[] = 'author';
               $c[] = 'author-' . $author->user_nicename;
       }

       // Category name classes for BODY on category archvies
       elseif ( is_category() ) {
               $cat = $wp_query->get_queried_object();
               $c[] = 'category';
               $c[] = 'category-' . $cat->slug;
       }

       // Tag name classes for BODY on tag archives
       elseif ( is_tag() ) {
               $tags = $wp_query->get_queried_object();
               $c[] = 'tag';
               $c[] = 'tag-' . $tags->slug;
       }

       // Page author for BODY on 'pages'
       elseif ( is_page() ) {
               $pageID = $wp_query->post->ID;
               $page_children = wp_list_pages("child_of=$pageID&echo=0");
               the_post();
               $c[] = 'page pageid-' . $pageID;
               $c[] = 'page-author-' . sanitize_title_with_dashes(strtolower(get_the_author('login')));
               // Checks to see if the page has children and/or is a child page; props to Adam
               if ( $page_children != '' )
                       $c[] = 'page-parent';
               if ( $wp_query->post->post_parent )
                       $c[] = 'page-child parent-pageid-' . $wp_query->post->post_parent;
               if ( is_page_template() )
                       $c[] = 'page-template page-template-' . str_replace( '.php', '-php', get_post_meta( $pageID, '_wp_page_template', true ) );
			   rewind_posts();
       }

       // For when a visitor is logged in while browsing
       if ( $current_user->ID )
               $c[] = 'loggedin';

       // Paged classes; for 'page X' classes of index, single, etc.
       if ( ( ( $page = $wp_query->get('paged') ) || ( $page = $wp_query->get('page') ) ) && $page > 1 ) {
               $c[] = 'paged-' . $page;
               if ( is_single() ) {
                       $c[] = 'single-paged-' . $page;
               } elseif ( is_page() ) {
                       $c[] = 'page-paged-' . $page;
               } elseif ( is_category() ) {
                       $c[] = 'category-paged-' . $page;
               } elseif ( is_tag() ) {
                       $c[] = 'tag-paged-' . $page;
               } elseif ( is_date() ) {
                       $c[] = 'date-paged-' . $page;
               } elseif ( is_author() ) {
                       $c[] = 'author-paged-' . $page;
               } elseif ( is_search() ) {
                       $c[] = 'search-paged-' . $page;
               }
       }

       // Separates classes with a single space, collates classes for BODY
       $c = join( ' ', apply_filters( 'body_class',  $c ) );

       // And tada!
       return $print ? print($c) : $c;
}

// Generates semantic classes for each post DIV element
function sandbox_post_class( $print = true ) {
       global $post, $sandbox_post_alt;

       // hentry for hAtom compliace, gets 'alt' for every other post DIV, describes the post type and p[n]
       $c = array( 'hentry', "p$sandbox_post_alt", $post->post_type, $post->post_status );

       // Author for the post queried
       $c[] = 'author-' . sanitize_title_with_dashes(strtolower(get_the_author('login')));

       // Category for the post queried
       foreach ( (array) get_the_category() as $cat )
               $c[] = 'category-' . $cat->slug;

       // Tags for the post queried; if not tagged, use .untagged
       if ( get_the_tags() == null ) {
               $c[] = 'untagged';
       } else {
               foreach ( (array) get_the_tags() as $tag )
                       $c[] = 'tag-' . $tag->slug;
       }

       // For password-protected posts
       if ( $post->post_password )
               $c[] = 'protected';

       // Applies the time- and date-based classes (below) to post DIV
       sandbox_date_classes( mysql2date( 'U', $post->post_date ), $c );

       // If it's the other to the every, then add 'alt' class
       if ( ++$sandbox_post_alt % 2 )
               $c[] = 'alt';

       // Separates classes with a single space, collates classes for post DIV
       $c = join( ' ', apply_filters( 'post_class', $c ) );

       // And tada!
       return $print ? print($c) : $c;
}

// Define the num val for 'alt' classes (in post DIV and comment LI)
$sandbox_post_alt = 1;

// Generates semantic classes for each comment LI element
function sandbox_comment_class( $print = true ) {
       global $comment, $post, $sandbox_comment_alt;

       // Collects the comment type (comment, trackback),
       $c = array( $comment->comment_type );

       // Counts trackbacks (t[n]) or comments (c[n])
       if ( $comment->comment_type == 'comment' ) {
               $c[] = "c$sandbox_comment_alt";
       } else {
               $c[] = "t$sandbox_comment_alt";
       }

       // If the comment author has an id (registered), then print the log in name
       if ( $comment->user_id > 0 ) {
               $user = get_userdata($comment->user_id);

               // For all registered users, 'byuser'; to specificy the registered user, 'commentauthor+[log in name]'
               $c[] = 'byuser comment-author-' . sanitize_title_with_dashes(strtolower( $user->user_login ));
               // For comment authors who are the author of the post
               if ( $comment->user_id === $post->post_author )
                       $c[] = 'bypostauthor';
       }

       // If it's the other to the every, then add 'alt' class; collects time- and date-based classes
       sandbox_date_classes( mysql2date( 'U', $comment->comment_date ), $c, 'c-' );
       if ( ++$sandbox_comment_alt % 2 )
               $c[] = 'alt';


       // Separates classes with a single space, collates classes for comment LI
       $c = join( ' ', apply_filters( 'comment_class', $c ) );

       // Tada again!
       return $print ? print($c) : $c;
}

// Generates time- and date-based classes for BODY, post DIVs, and comment LIs; relative to GMT (UTC)
function sandbox_date_classes( $t, &$c, $p = '' ) {
       $t = $t + ( get_option('gmt_offset') * 3600 );
       $c[] = $p . 'y' . gmdate( 'Y', $t ); // Year
       $c[] = $p . 'm' . gmdate( 'm', $t ); // Month
       $c[] = $p . 'd' . gmdate( 'd', $t ); // Day
       $c[] = $p . 'h' . gmdate( 'H', $t ); // Hour
}

// For category lists on category archives: Returns other categories except the current one (redundant)
function sandbox_cats_meow($glue) {
       $current_cat = single_cat_title( '', false );
       $separator = "\n";
       $cats = explode( $separator, get_the_category_list($separator) );

       foreach ( $cats as $i => $str ) {
               if ( strstr( $str, ">$current_cat<" ) ) {
                       unset($cats[$i]);
                       break;
               }
       }

       if ( empty($cats) )
               return false;

       return trim(join( $glue, $cats ));
}

// For tag lists on tag archives: Returns other tags except the current one (redundant)
function sandbox_tag_ur_it($glue) {
       $current_tag = single_tag_title( '', '',  false );
       $separator = "\n";
       $tags = explode( $separator, get_the_tag_list( "", "$separator", "" ) );

       foreach ( $tags as $i => $str ) {
               if ( strstr( $str, ">$current_tag<" ) ) {
                       unset($tags[$i]);
                       break;
               }
       }

       if ( empty($tags) )
               return false;

       return trim(join( $glue, $tags ));
}

// Produces an avatar image with the hCard-compliant photo class
function sandbox_commenter_link() {
       $commenter = get_comment_author_link();
       if ( ereg( '<a[^>]* class=[^>]+>', $commenter ) ) {
               $commenter = ereg_replace( '(<a[^>]* class=[\'"]?)', '\\1url ' , $commenter );
       } else {
               $commenter = ereg_replace( '(<a )/', '\\1class="url "' , $commenter );
       }
       $avatar_email = get_comment_author_email();
       $avatar_size = apply_filters( 'dkret_avatar_size', '40' ); // Filter for changing avatar size
	   $avatar_default = apply_filters( 'dkret_avatar_default', 'default' ); // Filter for avatar default image (monsterid, identicon , wawatar, default or path to own image)
       $avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( $avatar_email, $avatar_size, $avatar_default ) );
       echo $avatar . ' <span class="fn n">' . $commenter . '</span>';
}

// let dkret add related posts 
if (function_exists('st_related_posts')) {
  remove_filter('the_content','inlinePostTags');
  remove_filter('the_content','inlineRelatedPosts');
}

add_action('wp_head','dkret_inline_css');
add_action('admin_notices', 'dkret_update_dashboard', 4 );

// Adds filters so that things run smoothly
add_filter( 'archive_meta', 'wptexturize' );
add_filter( 'archive_meta', 'convert_smilies' );
add_filter( 'archive_meta', 'convert_chars' );
add_filter( 'archive_meta', 'wpautop' );

?>